home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Hacks / [√] May be freely distributed / Peter Di Camillo / MPW Enet Info / enet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-25  |  7.6 KB  |  385 lines  |  [TEXT/MPS ]

  1. #include <QuickDraw.h>
  2. #include <Menus.h>
  3. #include <Dialogs.h>
  4. #include <Memory.h>
  5. #include <SegLoad.h>
  6. #include <Events.h>
  7. #include <OSEvents.h>
  8. #include <TextEdit.h>
  9. #include <ToolUtils.h>
  10. #include <Desk.h>
  11. #include <Windows.h>
  12. #include <Fonts.h>
  13. #include <Files.h>
  14. #include <Devices.h>
  15. #include <Serial.h>
  16. #include <Resources.h>
  17. #include <Packages.h>
  18. #include <Errors.h>
  19. #include <String.h>
  20. #include <Strings.h>
  21.  
  22. #define    NMENUS        3        /* number of defined menus */
  23. #define appleMenu    256
  24. #define    fileMenu    257
  25. #define editMenu    258
  26. #define fileMSize    3        /* size of File menu */
  27.  
  28. #define DEFAULTNUM    129        /* default resource number */
  29.  
  30. char done;                    /* true to exit program */
  31. char debug_mode;            /* debug mode flag */
  32.  
  33.                             /* screen control rectangles */
  34.                             /* variables for my window */
  35. MenuHandle myMenus[NMENUS];
  36. struct EventRecord myEvent;
  37. char da_menu;
  38. Rect screenRect;            /* rectangle defining screen */
  39. Rect dragRect;                /* rectangle defining bounds for dragging */
  40. Rect sizeRect;                /* rectangle for resizing windows */
  41. struct Point sfgpoint;        /* standard file dialog locations */
  42. struct Point sfppoint;
  43.  
  44. void macinit(void);
  45. void macend(void);
  46. void hndmac(void);
  47. void updevent(GrafPtr);
  48. void menu_upd(void);
  49. void docommand(long);
  50. void appl_menu(void);
  51. void showerr(unsigned char * s);
  52. void aboutdlg(void);
  53. void ctrwindow(GrafPtr);
  54. void framedflt(DialogPtr);
  55.  
  56. /* In showerr.c: */
  57. void showerr(unsigned char * s);
  58. void ctralrt(short);
  59.  
  60. void getinfo(Boolean debug);
  61.  
  62. main()
  63. {
  64. debug_mode = 0;
  65. macinit();                    /* initialize Mac */
  66. done = 0;
  67. while (!done) hndmac();        /* handle Mac events and I/O */
  68. macend();
  69. }
  70.  
  71. void macinit()
  72. {
  73. short i, bigscreen;
  74.  
  75.             /* set-up general Macintosh environment */
  76. MaxApplZone();                /* set-up for efficient storage use */
  77. for (i=0; i < 4; i++) MoreMasters();
  78. InitGraf(&qd.thePort);
  79. FlushEvents(everyEvent, 0);
  80. InitWindows();
  81. InitFonts();
  82. InitMenus();
  83. InitDialogs(0L);
  84. InitCursor();
  85. TEInit();
  86.  
  87.             /* set-up menus */
  88. for (i=0; i < NMENUS; i++) {
  89.     myMenus[i] = GetMenu(256+i);
  90.     InsertMenu(myMenus[i], 0);
  91.     }
  92. AddResMenu(myMenus[0], 'DRVR');    
  93. da_menu = 0;
  94. appl_menu();
  95.  
  96.             /* center alerts */
  97. ctralrt(256);
  98.  
  99. screenRect = qd.screenBits.bounds;
  100.  
  101. bigscreen = (((screenRect.bottom-screenRect.top) >= 480) &&
  102.              ((screenRect.right-screenRect.left) >= 640)); 
  103.  
  104.             /* calculate standard file dialog location*/
  105. sfppoint.h = (screenRect.right-screenRect.left-304)/2;
  106. sfgpoint.h = (screenRect.right-screenRect.left-348)/2;
  107. if (bigscreen) {
  108.     sfppoint.v = (screenRect.bottom-screenRect.top-184)/3;
  109.     sfgpoint.v = (screenRect.bottom-screenRect.top-200)/3;
  110.     }
  111. else {
  112.     sfppoint.v = (screenRect.bottom-screenRect.top-184)/2;
  113.     sfgpoint.v = (screenRect.bottom-screenRect.top-200)/2;
  114.     }
  115.  
  116. SetRect(&dragRect, 0, 24, screenRect.right-4, screenRect.bottom-4);
  117.  
  118. SetRect(&sizeRect, 50, 25, screenRect.right+1, screenRect.bottom+1);
  119. }
  120.  
  121. void macend()
  122. {
  123. }
  124.  
  125. void hndmac()
  126. {
  127. OSErr rc;
  128. short code;
  129. GrafPtr gp;
  130. unsigned short h, w;
  131. long l;
  132. WindowPtr whichWindow;
  133. struct WindowRecord * fw;
  134. char frontda;
  135.  
  136. rc = GetNextEvent(everyEvent, &myEvent);
  137. if (rc == 0)                        /* FALSE from GNE */
  138.     switch(myEvent.what) {
  139.         case nullEvent:
  140.                 fw = (WindowRecord *)FrontWindow();
  141.                 frontda = fw->windowKind < 0;
  142.                 if (frontda) {
  143.                     if (!da_menu) {
  144.                         da_menu = 1;
  145.                         SetItem(myMenus[1], fileMSize, "\pClose");
  146.                         appl_menu();
  147.                         }
  148.                     }
  149.                 else {
  150.                     if (da_menu) {
  151.                         da_menu = 0;
  152.                         SetItem(myMenus[1], fileMSize, "\pQuit");
  153.                         appl_menu();
  154.                         }
  155.                     }
  156.                 SystemTask();    /* run DAs, etc. */
  157.                 break;
  158.         default:
  159.                 return;
  160.         }
  161. else switch(myEvent.what) {            /* TRUE from GNE */
  162.         case mouseDown:
  163.             code = FindWindow(myEvent.where, &whichWindow);
  164.             switch (code) {
  165.             case inMenuBar:
  166.                 menu_upd();
  167.                 docommand(MenuSelect(myEvent.where));
  168.                 break;
  169.             case inSysWindow:
  170.                 SystemClick((EventRecord *)&myEvent, whichWindow);
  171.                 break;
  172.             case inDrag:
  173.                 if (whichWindow == FrontWindow())
  174.                     DragWindow(whichWindow, myEvent.where, (Rect *)&dragRect);
  175.                 else SelectWindow(whichWindow);
  176.                 break;
  177.             case inGoAway:
  178.                 break;
  179.             case inGrow:
  180.                 if (whichWindow == FrontWindow()) {
  181.                     GetPort(&gp);
  182.                     SetPort((GrafPtr)whichWindow);
  183.                     l = GrowWindow(whichWindow, myEvent.where, (Rect *)&sizeRect);
  184.                     h = l >> 16;
  185.                     w = l & 0x0000ffffL;
  186.                     SizeWindow(whichWindow, w, h, 0x100);
  187.                     SetPort(gp);
  188.                     }
  189.                 break;
  190.             case inContent:
  191.                 if (whichWindow != FrontWindow()) {
  192.                     SelectWindow(whichWindow);
  193.                     break;
  194.                     }
  195.                 break;
  196.  
  197.             case inZoomIn:
  198.             case inZoomOut:
  199.                 if (TrackBox(whichWindow, myEvent.where, code)) {
  200.                     if (((WindowPeek)whichWindow)->dataHandle == 0) break;
  201.                     GetPort(&gp);
  202.                     SetPort((GrafPtr)whichWindow);
  203.                     EraseRect(&(((WindowRecord *)whichWindow)->port.portRect));
  204.                     ZoomWindow(whichWindow, code, 0);
  205.                     SetPort(gp);
  206.                     }
  207.                 break;
  208.  
  209.             default:
  210.                 break;
  211.             }
  212.             break;
  213.  
  214.         case mouseUp:
  215.             break;
  216.  
  217.         case keyDown:
  218.         case autoKey:
  219.             break;
  220.  
  221.         case activateEvt:
  222.             break;
  223.  
  224.         case updateEvt:
  225.             updevent((GrafPtr)myEvent.message);
  226.             break;
  227.  
  228.         case app4Evt:
  229.             break;
  230.  
  231.         default:    break;
  232.         }
  233. }
  234.  
  235. void updevent(msgptr)
  236. GrafPtr msgptr;
  237. {
  238. GrafPtr gp;
  239.  
  240. GetPort(&gp);
  241. SetPort(msgptr);
  242. BeginUpdate(msgptr);
  243. EndUpdate(msgptr);
  244. SetPort(gp);
  245. }
  246.  
  247. void menu_upd()
  248. {
  249. short i;
  250.  
  251. if (da_menu) {
  252.     for (i = 1; i < fileMSize; i++)
  253.         DisableItem(myMenus[1], i);
  254.     CheckItem(myMenus[1], fileMSize, 0);
  255.     }
  256. else {
  257.     CheckItem(myMenus[1], fileMSize, done << 8);
  258.     }
  259. }
  260.  
  261. void docommand(mResult)
  262. long mResult;
  263. {
  264. register short theItem, theMenu;
  265. char name[40];
  266. WindowRecord * fw;
  267. short i;
  268. static char nullstr[1] = {0};
  269. OSErr readmap();
  270.  
  271. theMenu = mResult >> 16;
  272. theItem = mResult & 0xff;
  273. switch(theMenu) {
  274.     case appleMenu:
  275.         if (theItem == 2) break;
  276.         if (theItem == 1) {
  277.             aboutdlg();
  278.             break;
  279.             }
  280.         GetItem(myMenus[0], theItem, name);
  281.         OpenDeskAcc(name);
  282.         break;
  283.     case fileMenu:
  284.         switch (theItem) {
  285.             case 1:
  286.                     debug_mode ^= 1;
  287.                     CheckItem(myMenus[1], 1, debug_mode);
  288.                     break;
  289.             case 2:
  290.                     getinfo(debug_mode);
  291.                     break;
  292.             case 3:
  293.                     if (!da_menu) {
  294.                         done ^= 1;
  295.                         break;
  296.                         }
  297.                     else {
  298.                         fw = (WindowRecord *)FrontWindow();
  299.                         i = fw->windowKind;
  300.                         if (i<0) CloseDeskAcc(i);
  301.                         }
  302.             default: break;
  303.             }
  304.         break;
  305.     case editMenu:
  306.         SystemEdit(theItem-1);
  307.         break;
  308.     default:
  309.         break;
  310.     }
  311.     HiliteMenu(0);
  312.     appl_menu();
  313. }
  314.  
  315. void appl_menu()        /* enable correct application menus */
  316. {
  317. static char last_da = 2;    /* init to invalid value */
  318.  
  319. /* skip drawing menu if no changes from last time */
  320. if (last_da == da_menu) return;
  321. last_da = da_menu;        /* save values for next time */
  322.  
  323. if (da_menu) {
  324.     EnableItem(myMenus[2], 0);
  325.     }
  326. else {
  327.     DisableItem(myMenus[2], 0);
  328.     }
  329. DrawMenuBar();
  330. }
  331.  
  332. void aboutdlg()
  333. {
  334. DialogPtr dlgptr;
  335. short itemHit;
  336. unsigned char **version;
  337. static char nullstr[1] = {0};
  338.  
  339. version = GetResource('EINF', 1);
  340. if (version == 0L) {
  341.     return;
  342.     }
  343. dlgptr = GetNewDialog(257, (Ptr)0L, (WindowPtr)-1L);
  344. ctrwindow(dlgptr);
  345. ParamText(*version, nullstr, nullstr, nullstr);
  346. ShowWindow(dlgptr);
  347.  
  348. /* frame the default selection */
  349. framedflt(dlgptr);
  350.  
  351. ModalDialog(0L, &itemHit);
  352. DisposDialog(dlgptr);
  353. ParamText(nullstr, nullstr, nullstr, nullstr);
  354. }
  355.  
  356. void ctrwindow(wp)
  357. GrafPtr wp;
  358. {
  359. short scrhsize, scrvsize;
  360. short whsize, wvsize;
  361.  
  362. scrhsize = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
  363. scrvsize = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top;
  364. whsize = wp->portRect.right-wp->portRect.left;
  365. wvsize = wp->portRect.bottom - wp->portRect.top;
  366. MoveWindow(wp, (scrhsize-whsize)/2, (scrvsize-wvsize)/3, 0);
  367. }
  368.  
  369. void framedflt(dlgptr)
  370. DialogPtr dlgptr;
  371. {
  372. short gtype;
  373. Handle gitem;
  374. Rect gbox;
  375. GrafPtr gp;
  376.  
  377. GetDItem(dlgptr, 1, >ype, &gitem, &gbox);
  378. GetPort(&gp);
  379. SetPort(dlgptr);
  380. PenSize(3,3);
  381. InsetRect(&gbox, -4, -4);
  382. FrameRoundRect(&gbox, 16, 16);
  383. SetPort(gp);
  384. }
  385.